#include<bits/stdc++.h>
#define int long long
#define N 1000005
#define INF 1e18
using namespace std;
int T,n,a[N],b[N],c[N],dp[2][N],mx[2],tot,ans,sum;
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>T;
    while(T--){
        cin>>n,sum=0;
        for(int i=1;i<=n;i++) cin>>a[i],c[i]=b[i]=a[i];
        sort(a+1,a+n+1);
        tot=unique(a+1,a+n+1)-(a+1);
        for(int i=1;i<=n;i++){
            b[i]=lower_bound(a+1,a+tot+1,b[i])-a;
        }
        for(int j=1;j<=tot;j++){
            dp[0][j]=dp[1][j]=-INF;
        }
        dp[1][0]=mx[0]=mx[1]=0;
        for(int i=2;i<=n;i++){
            for(int j=0;j<=tot;j++){
                dp[i&1][j]=dp[(i-1)&1][j]+(b[i]==b[i-1])*c[i];
                if(j==b[i-1]){
                    dp[i&1][j]=max(dp[i&1][j],max(mx[(i-1)&1],dp[(i-1)&1][b[i]]+c[i]));
                }
                mx[i&1]=max(mx[i&1],dp[i&1][j]);
            }
        }
        ans=0;
        for(int i=1;i<=tot;i++) ans=max(ans,dp[n&1][i]);
        cout<<ans<<endl;
    }
    return 0;
}